﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Stark_VK_Video
{
    public partial class MainForm : Form
    {
        private Point mouseOffset;
        private bool isMouseDown = false;
        VK_API api;
        public string[][] my_video_data = new string[1][];
        public string[][] search_video_data = new string[1][];

        public VideoPlayer player = new VideoPlayer();


        public MainForm()
        {
            InitializeComponent();
            api = new VK_API(this);
            if (api.Authorize()) 
            {
                CheckProfile();
            };
        }

        private void ToolPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }

        private void ToolPanel_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;

            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                yOffset = -e.Y - SystemInformation.CaptionHeight -
                    SystemInformation.FrameBorderSize.Height;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }
        }

        private void ToolPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

        private void CheckProfile() 
        {
            string[] prof = api.GetMyProfile();
            AutorizeButton.Visible = false;

            UserImg.ImageLocation = prof[2];
            UserFirstName.Text = prof[0];
            UserLastName.Text = prof[1];
            UserImg.Visible = true;
            UserFirstName.Visible = true;
            UserLastName.Visible = true;

            my_video_data = api.GetMyVideos();

            int video_count = (int)my_video_data.Length;

            MyVideoList.Items.Clear();
            for (int i = 1; i < video_count; i++)
            {
                MyVideoList.Items.Add(my_video_data[i][0]);

                /*
                video_data[i] = new string[3];
                video_data[i][0] = (string)response[i]["title"];
                video_data[i][1] = (string)response[i]["image"];
                video_data[i][2] = (string)response[i]["player"];*/
            }

            


        }

        private void AutorizeButton_Click(object sender, EventArgs e)
        {
            CheckProfile();
        }

        private void CloseAppButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MyVideoList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (MyVideoList.SelectedItem != null)
            {
                if (MyVideoList.SelectedItem.ToString().Length != 0)
                {
                    
                    int video_local_id = MyVideoList.SelectedIndex;
                    WebBrowser videobrowser = (WebBrowser)player.Controls["VideoPlayerBrowser"];
                    string video_url = (string)my_video_data[video_local_id + 1][2];
                    if (video_url != "")
                    {
                        videobrowser.Navigate(video_url);
                    }
                    player.Owner = this;
                    player.Show();
                }
            }
        }

        private void MyVideoList_MouseClick(object sender, MouseEventArgs e)
        {
            if (MyVideoList.SelectedItem != null)
            {
                if (MyVideoList.SelectedItem.ToString().Length != 0)
                {
                    int video_local_id = MyVideoList.SelectedIndex;
                    VideoPreviewBox.ImageLocation = my_video_data[video_local_id + 1][1];
                    PlayButton.Visible = true;
                }
            }
        }

        private void PlayButton_Click(object sender, EventArgs e)
        {
            if (MyVideoList.SelectedItem != null)
            {
                if (MyVideoList.SelectedItem.ToString().Length != 0)
                {

                    int video_local_id = MyVideoList.SelectedIndex;
                    WebBrowser videobrowser = (WebBrowser)player.Controls["VideoPlayerBrowser"];
                    string video_url = (string)my_video_data[video_local_id + 1][2];
                    if (video_url != "")
                    {
                        videobrowser.Navigate(video_url);
                    }
                    player.Owner = this;
                    player.Show();
                }
            }
        }

        private void SearchVideoButton_Click(object sender, EventArgs e)
        {
            string search_line = SearchVideoInput.Text;

            search_video_data = api.GetSearchVideos(search_line);

            int video_count = (int)search_video_data.Length;

            SearchVideoResults.Items.Clear();
            for (int i = 1; i < video_count; i++)
            {
                SearchVideoResults.Items.Add(search_video_data[i][0]);
            }
        }

        private void SearchVideoResults_MouseClick(object sender, MouseEventArgs e)
        {
            if (SearchVideoResults.SelectedItem != null)
            {
                if (SearchVideoResults.SelectedItem.ToString().Length != 0)
                {
                    int video_local_id = SearchVideoResults.SelectedIndex;

                    SearchVideoPreview.ImageLocation = search_video_data[video_local_id + 1][1];
                    SearchVideoPlayButton.Visible = true;
                }
            }
        }

        private void SearchVideoPlayButton_Click(object sender, EventArgs e)
        {
            if (SearchVideoResults.SelectedItem != null)
            {
                if (SearchVideoResults.SelectedItem.ToString().Length != 0)
                {

                    int video_local_id = SearchVideoResults.SelectedIndex;
                    WebBrowser videobrowser = (WebBrowser)player.Controls["VideoPlayerBrowser"];
                    string video_url = (string)search_video_data[video_local_id + 1][2];
                    if (video_url != "")
                    {
                        videobrowser.Navigate(video_url);
                    }
                    player.Owner = this;
                    player.Show();
                }
            }
        }
    }
}
